package com.richard.demo.controller;

import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;



public class MongoDB {

    public static void main(String[] args) {
        // 连接本地MongoDB，默认端口27017
        MongoClient mongoClient = new MongoClient(new ServerAddress("192.168.218.128", 27017));



        // 访问数据库
        MongoDatabase database = mongoClient.getDatabase("OwnDatabaseName");

//        // 访问集合
//        com.mongodb.client.MongoCollection<Document> collection = database.getCollection("OwnCollectionName");
//
//        // 插入文档
//        Document doc = new Document("name", "John").append("age", 30);
//        Document doc1 = new Document("name", "陈一铭").append("age", 20);
//        collection.insertOne(doc);
//
//        //删除文档
//        collection.deleteOne(doc);

        // 不再需要客户端时关闭连接
        mongoClient.close();
    }
}
